package mrirnaproject;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
/*import java.io.FileReader;*/
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

import org.biojava3.core.sequence.DNASequence;
import org.biojava3.core.sequence.RNASequence;
import org.biojava3.core.sequence.compound.NucleotideCompound;
import org.biojava3.core.sequence.template.SequenceView;

public class AllInputFiles {



	
	public String RefFile() throws IOException {


		//prompt user to input the various files
		Scanner sc = new Scanner(System.in);

		//User puts in Reference gDNA file (.fasta)
		String subPattern = "";
		Scanner input = null;

		System.out.println("Input path of Reference file:");



		boolean isFile = false;


		while (isFile == false){    

			String fileName = sc.next();
			File inputFile = new File(fileName);

			//Checks if file exits
			//If yes, reads file
			if (inputFile.exists()){
				try {
					input = new Scanner(inputFile);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				isFile = true;
			} 

			//Parses header if applicable
			//char c = input.nextLine().charAt(0);


			while(input.hasNextLine()){
				if( input.nextLine().startsWith(">")) {
					input.nextLine();
					subPattern = subPattern + input.nextLine(); 
				}
				subPattern = subPattern + input.nextLine();
			}
		} input.close();


		System.out.println("Subpattern: " + subPattern);


		//Converts file to DNA sequence
		//Converts DNA to RNA
		//Takes Reverse complement of RNA query
		DNASequence DNAfile = new DNASequence(subPattern);
		RNASequence RNAfile = DNAfile.getRNASequence();
		SequenceView<NucleotideCompound> compRNA = RNAfile.getReverseComplement();

		System.out.println(RNAfile.toString());
		System.out.println(compRNA.toString());

		/*end of processing the Ref gDNA file*/

		//User puts in file with the miRNA(.txt)
		System.out.println("Input path of miRNA file:");

		Scanner input_again = null;

		String miRNA = null;

		boolean isFile2 = false;

		//Checks if file exits
		//If yes, reads file
		while (isFile2 == false){    

			String fileName2 =sc.next();
			File inputFile2 = new File(fileName2);

			if (inputFile2.exists()){
				try {
					input_again = new Scanner(inputFile2);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				isFile2 = true;
			}
		}

		Queue<String> myQ = new LinkedList<String>();


		while(input_again.hasNextLine()){
			miRNA = input_again.nextLine();
			myQ.add(miRNA);
			myQ.remove("");
		} 


		input_again.close();

		String[] stringArr = myQ.toArray(new String[0]);
		System.out.println(Arrays.toString(stringArr));

		/* end of processing the miRNA file */

		//Prompts user to enter the scientic name of the organism the reference genome, if applicable
		System.out.println("Note: Download all available/updated miRNA sequences for reference organism from miRBase to user directory given below [Refer to README file]");
		System.out.println(System.getProperty("user.dir"));
		System.out.println("Please enter the scientific name of Reference Organism or N/A if not applicable:");	  
		
		boolean isRef = false;
		String Orgname;
		String gName;
		String sName;
		Scanner input3= null;
		
		while (isRef == false){
		gName = sc.next();
		sName = sc.next();
		Orgname = gName + " " + sName + ".fasta";
		
		//If file not available, user puts N/A and moves on to the next step
		if (Orgname == "no file.fasta"){
			break;
			
		}else{
			File Refan = new File(Orgname);

			//Checks if filename exists
			//If yes, reads file
			if (Refan.exists()){
				try {
					input3 = new Scanner(Refan);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				isRef = true;
			} 


			String subPattern2 = "";

			//reads only lines that don't belong to the header and puts into AnnotatedmiRNA.txt
			String result2 = "AnnotatedmiRNA.txt";
			FileWriter report2 = new FileWriter(result2);
			BufferedWriter Annreport = new BufferedWriter(report2);
			while(input3.hasNextLine()){
				if(input3.nextLine().startsWith(">")){
					subPattern2 = input3.nextLine();
					Annreport.write(subPattern2);
					Annreport.newLine();
				}


			}
			Annreport.close();
			
			input3.close();
			sc.close();
		}
		}
		
	

return "Done";
	}
}



